package com.mate.web;

import com.baomidou.mybatisplus.plugins.Page;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 继承类
 */
public class BaseController {


    @Resource
    protected HttpServletRequest request;

    @Resource
    protected HttpServletResponse response;


    /**
     * <p>
     * 获取分页对象
     * </p>
     *
     * @return
     */
    protected <T> Page<T> getPageOffset() {
        int _size;
        int _current;
        if (request.getParameter("limit") != null && !request.getParameter("limit").equals("")) {
            _size = Integer.parseInt(request.getParameter("limit"));
        } else {
            _size = 10;
        }
        if (request.getParameter("offset") != null && !request.getParameter("offset").equals("")) {
            _current = (Integer.parseInt(request.getParameter("offset")));
        } else {
            _current = 1;
        }
        Page<T> page = new Page<T>(_current, _size);
        return page;
    }

    public Date getTime(String time) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            return formatter.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }
}
